package com.deyuanyun.pic.settings.controller.prvlg;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.controller.dto.prvlg.UserGroupVO;
import com.deyuanyun.pic.settings.service.prvlg.UserGroupService;
import com.deyuanyun.pic.settings.service.prvlg.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/settings/userGroup")
public class UserGroupController {
	@Resource
	private UserGroupService userGroupService;
	@Resource
	private UserService userService;
	/**
	 * 跳转用户组管理主界面
	 * 
	 * @return
	 */
	@RequestMapping("/getUserGroupManagerIndex.html")
	public ModelAndView getUserGroupManagerIndex() {
		ModelAndView mv = new ModelAndView();
		List main = userGroupService.getUserGroupManagerIndex();
		mv.addObject("main", main);
		mv.setViewName("/settings/prvlg/userGroupManager");
		return mv;
	}
	
	/**
	 * 查询用户组
	 * @param ugv
	 */
	@RequestMapping("/searchUserGroup.json")
	@ResponseBody
	public void searchUserGroup(UserGroupVO ugv){
		String name = "";
		String code = "";
		if(!StringUtils.isEmpty(ugv.getName())){
			name = ugv.getName();
		}
		if(!StringUtils.isEmpty(ugv.getCode())){
			code = ugv.getCode();
		}
		List list = userGroupService.searchUserGroup(name,code);
		AjaxSupport.sendSuccessText("",list);
	}
	/**
	 * 独立清管 用户组列表
	 * @param ugv
	 */
	@RequestMapping("/userGroupList.json")
	@ResponseBody
	public void userGroupList(UserGroupVO ugv){
		String name = "";
		String code = "";
		if(!StringUtils.isEmpty(ugv.getName())){
			name = ugv.getName();
		}
		if(!StringUtils.isEmpty(ugv.getCode())){
			code = ugv.getCode();
		}
		PageJsonBean jsonBean = new PageJsonBean();
		List list = userGroupService.searchUserGroup(name,code);
		jsonBean.setList(list);
		AjaxSupport.sendSuccessText("", jsonBean);
	}
	
	/**
	 * 点击新增 或编辑时加载数据
	 * @param id
	 */
	@RequestMapping("/addOrModify.json")
	@ResponseBody
	public void addOrModify(String id){
		Map<String,Object> map = new HashMap<String,Object>();
		map = userGroupService.addOrModify(id);
		AjaxSupport.sendSuccessText("",map);
	}
	
	/**
	 * 保存，修改用户组或分配的用户权限
	 * @param ugv
	 */
	@RequestMapping("/saveGroup.json")
	@ResponseBody
	public void saveGroup(UserGroupVO ugv){
		Asserts.assertTrue(!StringUtils.isEmpty(ugv.getName()), "请输入用户组名称");
//		Asserts.assertTrue(!StringUtils.isEmpty(ugv.getCode()), "请输入用户组编号");
		Asserts.assertTrue(!StringUtils.isEmpty(ugv.getState()), "请设置状态");
		int i = 0;
		String name = ugv.getName();
		String code = ugv.getCode();
		String remark = ugv.getRemark();
		String id = ugv.getId();
		String state = ugv.getState();
		i = userGroupService.saveGroup(name,code,remark,id,state);
		if(i > 0){
			AjaxSupport.sendSuccessText("","1");
		}
	}
	/**
	 * 点击授权加载信息
	 * @param id
	 */
	@RequestMapping("/groupAuthorize.json")
	@ResponseBody
	public void groupAuthorize(String id){
		Asserts.assertTrue(!StringUtils.isEmpty(id), "数据异常，请刷新页面重试");
		Map<String,Object> map = new HashMap<String,Object>();
		map = userGroupService.groupAuthorize(id);
		AjaxSupport.sendSuccessText("",map);
	}
	
	/**
	 * 保存用户组和角色的授权信息
	 * @param id
	 * @param str
	 */
	@RequestMapping("/saveAuthorize.json")
	@ResponseBody
	public void saveAuthorize(String id,String str){
		String[] data = str.split(",");
    	if(data.length > 0 && !StringUtils.isEmpty(id)){
    		int i = userGroupService.saveAuthorize(id,data);
    		if(i > 0){
    			AjaxSupport.sendSuccessText("",i);
    		}
    	}
	}
	
	 /**
     * 删除用户组
     * @param idStr
     */
    @RequestMapping("/deleteData.json")
    @ResponseBody
    public void deleteData(String idStr){
    	Asserts.assertTrue(!StringUtils.isEmpty(idStr), "请勾选需要删除的数据");
    	int count = userGroupService.deleteData(idStr);
    	AjaxSupport.sendSuccessText("",count);
    }
    
    /**
     * 点击列表状态栏，改变用户组状态
     * @param uv
     * @throws Exception
     */
    @RequestMapping("/changeState.json")
    @ResponseBody
    public void changeState(String id,String state) throws Exception{
    	Asserts.assertTrue(!StringUtils.isEmpty(id), "数据异常，请刷新页面重试");
    	Asserts.assertTrue(!StringUtils.isEmpty(state), "数据异常，请刷新页面重试");
    	String userGroupId = id;
    	int i = userGroupService.changeState(userGroupId,state);
    	if(i > 0){
    		AjaxSupport.sendSuccessText("","1");
    	}
    }
    
    /**
     * 启用或停用用户组
     * @param idStr
     * @param opt
     */
    @RequestMapping("/dataOnOrOff.json")
    @ResponseBody
    public void dataOnOrOff(String idStr,String opt){
    	Asserts.assertTrue(!StringUtils.isEmpty(idStr), "请勾选需要操作的数据");
    	Asserts.assertTrue(!StringUtils.isEmpty(opt), "数据异常，请刷新页面重试");
    	int count = userGroupService.dataOnOrOff(idStr,opt);
    	AjaxSupport.sendSuccessText("",count);
    }
}
